home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 Applications 1996 May / SGI IRIX 6.2 Applications 1996 May.iso / dist / impr_dev.idb / usr / impressario / src / examples / libimp / imptobw.c.z / imptobw.c
C/C++ Source or Header  |  1996-05-06  |  4KB  |  145 lines

  1. /**************************************************************************
  2.  *
  3.  *           Copyright (c)    1993 Silicon Graphics, Inc.
  4.  *            All Rights Reserved
  5.  *
  6.  *       THIS    IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
  7.  *
  8.  * The copyright notice above does not evidence any actual of intended
  9.  * publication of such source code, and is an unpublished work by Silicon
  10.  * Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
  11.  * the property of Silicon Graphics, Inc. Any use, duplication or
  12.  * disclosure not specifically authorized by Silicon Graphics is strictly
  13.  * prohibited.
  14.  *
  15.  * RESTRICTED RIGHTS LEGEND:
  16.  *
  17.  * Use, duplication or disclosure by the Government is subject to
  18.  * restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
  19.  * Technical Data and Computer Software clause at DFARS 52.227-7013,
  20.  * and/or in similar or successor clauses in the FAR, DOD or NASA FAR
  21.  * Supplement. Unpublished - rights reserved under the Copyright Laws of
  22.  * the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
  23.  * Shoreline Blvd., Mountain View, CA 94039-7311
  24.  **************************************************************************
  25.  *
  26.  * File: imptobw.c
  27.  *
  28.  * Description: Converts an SGI Image file from RGB to black and white (W).
  29.  *
  30.  *    Uasge: imptobw inimage outimage
  31.  *
  32.  **************************************************************************/
  33.  
  34.  
  35. #ident "$Revision: 1.1 $"
  36.  
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include "imp.h"
  42.  
  43.  
  44. /**************************************************************************
  45.  *
  46.  * Function: main
  47.  *
  48.  * Description: Program entry point
  49.  *
  50.  * Parameters: 
  51.  *    argc (I) - number of command line arguments
  52.  *    argv (I) - command line arguments
  53.  *
  54.  * Return: 0 if no errors. 1 if errors.
  55.  *
  56.  **************************************************************************/
  57.  
  58. int main(int argc, char *argv[])
  59. {
  60.     IMPImage *inImage, *outImage;
  61.     register int y, chan;
  62.     register int xsize, ysize, inNumChan;
  63.     short *row[3], *wrow;
  64.     char *pname;
  65.  
  66.     /*
  67.      * Extract the program name
  68.      */
  69.     pname = ((pname = strrchr(argv[0], '/')) == NULL) ? argv[0]: pname + 1;
  70.  
  71.     /*
  72.      * Check the arg count
  73.      */
  74.     if (argc < 3) {
  75.     fprintf(stderr, "Usage: %s inimage outimage\n", pname);
  76.     exit(1);
  77.     } 
  78.  
  79.     /*
  80.      * Open the input image
  81.      */
  82.     if ((inImage = impOpen(argv[1], "r")) == NULL ) {
  83.     impPerror(pname);
  84.     exit(1);
  85.     }
  86.     xsize = impXSize(inImage);
  87.     ysize = impYSize(inImage);
  88.     inNumChan = impNumChannels(inImage);
  89.     for (chan = 0; chan < inNumChan; chan++)
  90.         row[chan] = (short*)malloc(xsize * sizeof(short));
  91.     wrow = (short*)malloc(xsize * sizeof(short));
  92.  
  93.     /*
  94.      * Open the output image
  95.      */
  96.     if ((outImage = impOpen(argv[2], "w",
  97.         impMakeRasterType(impRasterEncoding(inImage),
  98.                  impRasterBPP(inImage)),
  99.         impDimension(inImage),
  100.         xsize, ysize, 1,
  101.         impImageType(inImage),
  102.         impName(inImage))) == NULL) {
  103.     impPerror(pname);
  104.     exit(1);
  105.     }
  106.  
  107.     /*
  108.      * Convert the image. Note that if the image is already one
  109.      * channel just copy it.
  110.      */
  111.     for (y = 0; y < ysize; y++) {
  112.     for (chan = 0; chan < inNumChan; chan++) {
  113.         if (impReadRow(inImage, row[chan], y, chan) < 0) {
  114.             impPerror(pname);
  115.             exit(1);
  116.         }
  117.     }
  118.     if (inNumChan > 1)
  119.         impRGBtoW(row[0], row[1], row[2], wrow, xsize);
  120.     else
  121.         wrow = row[0];
  122.     if (impWriteRow(outImage, wrow, y, 0) < 0) {
  123.         impPerror(pname);
  124.         exit(1);
  125.     }
  126.     }
  127.  
  128.     /*
  129.      * Close up all images and free storage
  130.      */
  131.     for (chan = 0; chan < inNumChan; chan++)
  132.         free(row[chan]);
  133.     free(wrow);
  134.     if (impClose(inImage) < 0) {
  135.     impPerror(pname);
  136.     exit(1);
  137.     }
  138.     if (impClose(outImage) < 0) {
  139.     impPerror(pname);
  140.     exit(1);
  141.     }
  142.  
  143.     return 0;
  144. }
  145.